<?php defined('BASEPATH') OR exit('No direct script access allowed');

class CarSeat extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('carSeat_m');
    }

    public function index($page = 1)
    {
        //分页
        $limit                      = 20;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->carSeat_m->count_seats();
        $config['base_url']         = base_url('manage/CarSeat/index');
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $seat               = $this->carSeat_m->get_seats_list($start, $limit);
        if ($seat) {
            foreach ($seat as $key => $val) {
                $seat[$key]['car_level'] = $this->carSeat_m->get_car_level()[$val['car_level']];
            }
        }
        $data['seat'] = $seat;
        $this->load->view('carseat/index', $data);
    }

    public function add()
    {
        $car_level = $this->carSeat_m->get_car_level();
        $this->load->view('carseat/form', ['car_level' => $car_level]);
    }

    public function edit($seat_id = null)
    {
        $seat = $this->carSeat_m->find($seat_id);
        if (!$seat) {
            $this->error('参数错误！');
        }
        $car_level         = $this->carSeat_m->get_car_level();
        $data['seat']      = $seat;
        $data['car_level'] = $car_level;
        $this->load->view('carseat/form', $data);
    }

    public function del()
    {

        $get     = $this->input->get();
        $seat_id = $get['seat_id'];
        if ($seat_id) {
            if ($this->carSeat_m->delete($seat_id)) {
                $this->success('恭喜，删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }
    }

    public function update()
    {

        $post              = $this->input->post();
        $data['car_seat']  = $post['car_seat'];
        $data['car_level'] = $post['car_level'];
        $seat_id           = $post['seat_id'];
        if ($data['car_seat'] == '') {
            $this->error('坐席不能为空。');
        }
        if (!$seat_id) {
            $data['create_time'] = date("Y-m-d H:i:s", time());
            $this->carSeat_m->insert($data);
        } else {
            $this->carSeat_m->update(['id' => $seat_id], $data);
        }
        show_message('恭喜，操作成功！', site_url(X_URL . 'CarSeat/index'), 1);
    }

}

